#!/bin/bash
#
# Developed by Fred Weinhaus 7/16/2010 .......... revised 4/25/2015
#
# ------------------------------------------------------------------------------
# 
# Licensing:
# 
# Copyright © Fred Weinhaus
# 
# My scripts are available free of charge for non-commercial use, ONLY.
# 
# For use of my scripts in commercial (for-profit) environments or 
# non-free applications, please contact me (Fred Weinhaus) for 
# licensing arrangements. My email address is fmw at alink dot net.
# 
# If you: 1) redistribute, 2) incorporate any of these scripts into other 
# free applications or 3) reprogram them in another scripting language, 
# then you must contact me for permission, especially if the result might 
# be used in a commercial or for-profit environment.
# 
# My scripts are also subject, in a subordinate manner, to the ImageMagick 
# license, which can be found at: http://www.imagemagick.org/script/license.php
# 
# ------------------------------------------------------------------------------
# 
####
#
# USAGE: bevelborder [-s size] [-m method] [-p percent] [-c contrast] [-b bcolor] [-a amount] [-t type] infile outfile
# USAGE: bevelborder [-h or -help]
# 
# OPTIONS:
# 
# -s      size              size of border in pixels; same in both dimensions; 
#                           default equals 10% of min(imagewidth, imageheight)
# -m      method            bevel method; choices are: outer, inner or split;
#                           default=outer
# -p      percent           split percent between outer and inner bevel; 
#                           100 is outer bevel only; 0 is inner bevel only;
#                           default=50 applies only to method=split
# -c      contrast          contrast percent for bevel; 0<=integer<=100; 
#                           default=50
# -b      bcolor            border coloring; any IM opaque color is allowed; 
#                           default is no coloring
# -a      amount            amount of border coloring; 0<=integer<=100; 
#                           default=25
# -t      type              type of compose; hardlight, linearlight or 
#                           vividlight; default=hardlight
# 
###
# 
# NAME: BEVELBORDER 
# 
# PURPOSE: To applies a bevel effect to the border of an image.
# 
# DESCRIPTION: BEVELBORDER applies a bevel effect to the border of an image. 
# The bevel can be an outer bevel (raised effect), an inner bevel (depressed 
# effect) or a split (mix) of the two. The border may also be colorized.
# 
# 
# ARGUMENTS: 
# 
# -s size ... SIZE is the dimensions of the border region in pixels. The same 
# value is used in both dimensions. Values are integers greater than 0. The 
# default is 10% of the min(width,height) of the image.
# 
# -m method ... METHOD is the bevel method. Choices are: outer, which makes a
# raised effect; inner, which makes a depressed effect; and split, which is 
# part raised and part depressed. The amount of each is controlled by the 
# percent argument. The default is outer.
# 
# -p percent ... PERCENT is the percent split between outer and inner bevels.
# Values are integers such that 0<=percent<=100. A value of 100 is full outer 
# bevel. A value of 0 is full inner bevel. The default is 50 and only applies 
# when the method=split.
# 
# -c contrast ... CONTRAST percent for innerbevel or outerbevel. Values are
# integers between 0 and 100. The default=50.
# 
# -b bcolor ... BCOLOR is the bevel colorization color. Any valid opaque IM 
# color is allowed. The default is no additional colorization.
# 
# -a amount ... AMOUNT of colorization. Values are integers between 0 and 100. 
# The default=25.
#
# -t type ... TYPE of compose. Choices are: hardlight, linearlight and 
# vividlight. The default=hardlight
# 
# REQUIREMENTS: IM 6.5.9.0 or higher due to the use of -brightness-contrast.
# 
# CAVEAT: No guarantee that this script will work on all platforms, 
# nor that trapping of inconsistent parameters is complete and 
# foolproof. Use At Your Own Risk. 
# 
######
# 

# set default values
size=""					# bevel border amount in pixels
method="outer"			# outer, inner, split
percent=50				# split percent; 0 to 100; 100 is outer; 0 is inner
contrast=50				# bevel contrast percent
bcolor=""				# bevel color
amount=25				# amount of coloring; 0<=integer<=100
type="hardlight"	    # bevel compose method

# set directory for temporary files
dir="."    # suggestions are dir="." or dir="/tmp"

# set up functions to report Usage and Usage with Description
PROGNAME=`type $0 | awk '{print $3}'`  # search for executable on path
PROGDIR=`dirname $PROGNAME`            # extract directory of program
PROGNAME=`basename $PROGNAME`          # base name of program
usage1() 
	{
	echo >&2 ""
	echo >&2 "$PROGNAME:" "$@"
	sed >&2 -e '1,/^####/d;  /^###/g;  /^#/!q;  s/^#//;  s/^ //;  4,$p' "$PROGDIR/$PROGNAME"
	}
usage2() 
	{
	echo >&2 ""
	echo >&2 "$PROGNAME:" "$@"
	sed >&2 -e '1,/^####/d;  /^######/g;  /^#/!q;  s/^#*//;  s/^ //;  4,$p' "$PROGDIR/$PROGNAME"
	}


# function to report error messages
errMsg()
	{
	echo ""
	echo $1
	echo ""
	usage1
	exit 1
	}


# function to test for minus at start of value of second part of option 1 or 2
checkMinus()
	{
	test=`echo "$1" | grep -c '^-.*$'`   # returns 1 if match; 0 otherwise
    [ $test -eq 1 ] && errMsg "$errorMsg"
	}

# test for correct number of arguments and get values
if [ $# -eq 0 ]
	then
	# help information
   echo ""
   usage2
   exit 0
elif [ $# -gt 16 ]
	then
	errMsg "--- TOO MANY ARGUMENTS WERE PROVIDED ---"
else
	while [ $# -gt 0 ]
		do
			# get parameter values
			case "$1" in
		  -h|-help)    # help information
					   echo ""
					   usage2
					   exit 0
					   ;;
				-s)    # get size
					   shift  # to get the next parameter
					   # test if parameter starts with minus sign 
					   errorMsg="--- INVALID SIZE SPECIFICATION ---"
					   checkMinus "$1"
					   size=`expr "$1" : '\([0-9]*\)'`
					   [ "$size" = "" ] && errMsg "--- SIZE=$size MUST BE A NON-NEGATIVE INTEGER VALUE (with no sign) ---"
					   testA=`echo "$size <= 0" | bc`
					   [ $testA -eq 1 ] && errMsg "--- SIZE=$size MUST BE A POSITIVE INTEGER ---"
					   ;;
				-m)    # get method
					   shift  # to get the next parameter
					   # test if parameter starts with minus sign
					   errorMsg="--- INVALID METHOD SPECIFICATION ---"
					   checkMinus "$1"
					   # test type values
					   method=`echo "$1" | tr "[:upper:]" "[:lower:]"`
					   case "$method" in
							outer|inner|split) ;; # do nothing - valid type
							*)  errMsg "--- METHOD=$method IS NOT A VALID VALUE ---" ;;
					   esac
					   ;;
				-p)    # get percent
					   shift  # to get the next parameter
					   # test if parameter starts with minus sign 
					   errorMsg="--- INVALID PERCENT SPECIFICATION ---"
					   checkMinus "$1"
					   percent=`expr "$1" : '\([0-9]*\)'`
					   [ "$percent" = "" ] && errMsg "--- PERCENT=$percent MUST BE A NON-NEGATIVE INTEGER ---"
					   testA=`echo "$percent < 0" | bc`
					   testB=`echo "$percent > 100" | bc`
					   [ $testA -eq 1 -o $testB -eq 1 ] && errMsg "--- PERCENT=$percent MUST BE AN INTEGER BETWEEN 0 AND 100 ---"
					   ;;
				-c)    # get contrast
					   shift  # to get the next parameter
					   # test if parameter starts with minus sign 
					   errorMsg="--- INVALID CONTRAST SPECIFICATION ---"
					   checkMinus "$1"
					   contrast=`expr "$1" : '\([0-9]*\)'`
					   [ "$contrast" = "" ] && errMsg "--- CONTRAST=$contrast MUST BE A NON-NEGATIVE INTEGER ---"
					   testA=`echo "$contrast < 0" | bc`
					   testB=`echo "$contrast > 100" | bc`
					   [ $testA -eq 1 -o $testB -eq 1 ] && errMsg "--- CONTRAST=$contrast MUST BE AN INTEGER BETWEEN 0 AND 100 ---"
					   ;;
				-b)    # get bcolor
					   shift  # to get the next parameter
					   # test if parameter starts with minus sign 
					   errorMsg="--- INVALID BCOLOR SPECIFICATION ---"
					   checkMinus "$1"
					   bcolor="$1"
					   ;;
				-a)    # get amount
					   shift  # to get the next parameter
					   # test if parameter starts with minus sign 
					   errorMsg="--- INVALID AMOUNT SPECIFICATION ---"
					   checkMinus "$1"
					   amount=`expr "$1" : '\([0-9]*\)'`
					   [ "$amount" = "" ] && errMsg "--- AMOUNT=$amount MUST BE A NON-NEGATIVE INTEGER ---"
					   testA=`echo "$amount < 0" | bc`
					   testB=`echo "$amount > 100" | bc`
					   [ $testA -eq 1 -o $testB -eq 1 ] && errMsg "--- AMOUNT=$amount MUST BE AN INTEGER BETWEEN 0 AND 100 ---"
					   ;;
				-t)    # get type
					   shift  # to get the next parameter
					   # test if parameter starts with minus sign
					   errorMsg="--- INVALID TYPE SPECIFICATION ---"
					   checkMinus "$1"
					   # test type values
					   type=`echo "$1" | tr "[:upper:]" "[:lower:]"`
					   case "$type" in
							hardlight|linearlight|vividlight) ;; # do nothing - valid type
							*)  errMsg "--- METHOD=$method IS NOT A VALID VALUE ---" ;;
					   esac
					   ;;
				 -)    # STDIN and end of arguments
					   break
					   ;;
				-*)    # any other - argument
					   errMsg "--- UNKNOWN OPTION ---"
					   ;;
		     	 *)    # end of arguments
					   break
					   ;;
			esac
			shift   # next option
	done
	#
	# get infile and outfile
	infile="$1"
	outfile="$2"
fi

# test that infile provided
[ "$infile" = "" ] && errMsg "NO INPUT FILE SPECIFIED"

# test that outfile provided
[ "$outfile" = "" ] && errMsg "NO OUTPUT FILE SPECIFIED"


tmpA="$dir/bevelborder_$$.mpc"
tmpB="$dir/bevelborder_$$.cache"
trap "rm -f $tmpA $tmpB;" 0
trap "rm -f $tmpA $tmpB; exit 1" 1 2 3 15
trap "rm -f $tmpA $tmpB; exit 1" ERR

# read the input image into the TMP cached image.
convert -quiet "$infile" +repage "$tmpA" ||
  errMsg "--- FILE $infile NOT READABLE OR HAS ZERO SIZE ---"

# set default size
if [ "$size" = "" ]; then
	size=`convert $tmpA -ping -format "%[fx:floor(0.1*min(w,h))]" info:`
	wsize=$size
	hsize=$size
fi

# get input image size
ww=`convert $infile -ping -format "%w" info:`
hh=`convert $infile -ping -format "%h" info:`
wd=`convert xc: -format "%[fx:$ww-2*$size]" info:`
ht=`convert xc: -format "%[fx:$hh-2*$size]" info:`

# adjust contrast and transparency to fractions
contr1=`convert xc: -format "%[fx:$contrast-100]" info:`

# setup bevel parameters
if [ "$method" = "outer" ]; then
	wsize2=$size
	hsize2=0
elif [ "$method" = "inner" ]; then
	hsize2=$size
	wsize2=0
elif [ "$method" = "split" ]; then
	wsize2=`convert xc: -format "%[fx:floor($percent*$size/100)]" info:`
	hsize2=$(($size-$wsize2))
fi
#echo "ww=$ww; hh=$hh; wd=$wd; ht=$ht; wsize2=$wsize2; hsize2=$hsize2"

if [ "$bcolor" = "" ]; then
	colorize=""
else
	colorize="-fill $bcolor -colorize $amount%"
fi

# get im version
im_version=`convert -list configure | \
	sed '/^LIB_VERSION_NUMBER */!d; s//,/;  s/,/,0/g;  s/,0*\([0-9][0-9]\)/\1/g' | head -n 1`

# colorspace RGB and sRGB swapped between 6.7.5.5 and 6.7.6.7 
# though probably not resolved until the latter
# then -colorspace gray changed to linear between 6.7.6.7 and 6.7.8.2 
# then -separate converted to linear gray channels between 6.7.6.7 and 6.7.8.2,
# though probably not resolved until the latter
# so -colorspace HSL/HSB -separate and -colorspace gray became linear
# but we need to use -set colorspace RGB before using them at appropriate times
# so that results stay as in original script
# also some compose methods have not been fixed as of 6.7.8.7 such as hardlight
# The following was determined from various version tests using bevel.
# with IM 6.7.4.10, 6.7.6.10, 6.7.8.7
if [ "$im_version" -lt "06070606" -o "$im_version" -gt "06070707" ]; then
	cspace="RGB"
else
	cspace="sRGB"
fi
if [ "$im_version" -lt "06070607" -o "$im_version" -gt "06070707" ]; then
	setcspace="-set colorspace RGB"
else
	setcspace=""
fi
# no need for setcspace for grayscale or channels after 6.8.5.4
if [ "$im_version" -gt "06080504" ]; then
	setcspace=""
	cspace="sRGB"
fi


# process image
convert $tmpA -size ${wd}x${ht} xc:"gray(50%)" \
\( -clone 1 -frame ${size}x${size}+${wsize2}+${hsize2} \
-auto-level -black-threshold 25% -white-threshold 75% $colorize \
-brightness-contrast 0,${contr1}%  \
-clone 1 -gravity center -composite \) \
-delete 1 $setcspace -compose $type -composite \
"$outfile"

exit 0